
//
// 121230 - AH - Created.
//

package org.japril.swing.jutil.logging;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import org.japril.jutil.logging.demo.Demo;
import org.japril.swing.TestFrame;

/**
 * <p>
 * </p>
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class LoggingPanel extends JPanel 
{
  private final ControlLogSinkPanel m_clsp=new ControlLogSinkPanel();
  
  //private final HtmlLogPanel m_hlp=new HtmlLogPanel();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param uas {@link Action} provided by client.
   */
  public LoggingPanel(Action... uas)
  {
    setLayout(new BorderLayout());
    
    JTabbedPane tabs=new JTabbedPane();
    //tabs.addTab("HTML Log", m_hlp);
    tabs.addTab("Log Control", new JScrollPane(m_clsp));
    
    add(tabs, BorderLayout.CENTER);
    
    //hookUpToLogSystem();
    
    //makeToolBar(uas);
    
    /*
    try
    {
      final ControlLogSinks sinks=ControlLogSinks.getInstance();
      sinks.addChangeListener(
        new ChangeListener()
        {
          public void stateChanged(ChangeEvent e)
          {
            ControlLogSink curr=sinks.getCurrentSink();
            if (curr!=null) m_clsp.setSink(curr);
          }
        }
      );
    }
    catch (IOException iox)
    {
      iox.printStackTrace();
    }
  }
  
  private void makeToolBar(Action[] uas)
  {
    JToolBar bar=null;
    
    if (uas!=null) 
    {
      int n=uas.length;
      for (int i=0; i<n; i++)
      {
        Action ua=uas[i];
        if (ua==null)
        {
          throw new NullPointerException("uas["+i+"]");
        }
        
        if (bar==null) bar=new JToolBar();
        
        bar.add(ua);
      }
    }
    
    if (bar!=null) add(bar, BorderLayout.NORTH);
  }
  
  private void hookUpToLogSystem()
  {
    try
    {
      ControlLogSink cls=ControlLogSinks.getInstance().getCurrentSink();
      if (cls!=null)
      {
        cls.addSink(m_hlp);
      }
    }
    catch (IOException iox)
    {
      iox.printStackTrace();
    }
    /*
    LogSink ls=Log.getLogSink();
    if (ls instanceof ControlLogSink)
    {
      ControlLogSink cls=(ControlLogSink)ls;
      m_clsp.setSink(cls);
      cls.addSink(m_hlp);
    }
    */
  }
  
  public static void main(String[] args) 
  {
    LoggingPanel lp=new LoggingPanel
    (
      new AbstractAction("Test")
      {
        public void actionPerformed(ActionEvent e) 
        {
          new Demo().perform1();
        }
      }
    );
    
    TestFrame.show(lp, 300, 500);
  }
  
}
